package org.lee.nio.udp;

import sun.nio.ch.DatagramSocketAdaptor;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author jackielee
 * @version 1.0
 * @description UDPReceiver
 * @date 2021/3/8
 **/
public class UDPReceiver {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        // 接收数据
        byte[] buffer = new byte[1024];
        int length = buffer.length;
        DatagramPacket dp = new DatagramPacket(buffer, length);
        ds.receive(dp);
        // 解析数据
        InetAddress address = dp.getAddress();
        byte[] data = dp.getData();
        int len = dp.getLength();
        System.out.printf("received from %s: %s\n", address.toString(), new String(data, 0,len));
        dp.setData(new String("nice to meet you!").getBytes());
        ds.send(dp);
    }
}
